package org.xiaov.lunasql.engine.transform.interval;

import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntervalUnit;

/**
 * <p>
 * Quarter2MonthIntervalHandler 季度转换为月
 * </p>
 *
 * @author lzq
 * @since 2024/12/24 16:28
 */
public class Quarter2MonthIntervalHandler implements IntervalHandler {
    @Override
    public void interval(SQLIntervalExpr x) {
        x.setUnit(SQLIntervalUnit.MONTH);
        SQLExpr sqlExpr = x.getValue();
        if (sqlExpr instanceof SQLIntegerExpr number) {
            int month = Integer.parseInt(number.getNumber().toString()) * 3;
            x.setValue(new SQLCharExpr(month + ""));
        } else if (sqlExpr instanceof SQLCharExpr charExpr) {
            int month = Integer.parseInt(charExpr.getText()) * 3;
            x.setValue(new SQLCharExpr(month + ""));
        }
    }
}
